package com.yyh.common.util;

import java.util.Iterator;
import java.util.Objects;

/**
 * 字符串工具类
 * @author xuan
 * @date 2021/10/29
 */
public class StringUtils
{
    /** 判断一个字符串是否为空 */
    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    /** 重载 */
    public static boolean isEmpty(Object object) {
        return null != object ? isEmpty(String.valueOf(object)) : false;
    }

    /** 使用分隔符，将装有String类型的集合连缀起来 */
    public static String join(String delimiter, Iterable<? extends String> elements) {
        Objects.requireNonNull(delimiter);
        Objects.requireNonNull(elements);
        StringBuilder stringBuilder = new StringBuilder();
        Iterator iterator = elements.iterator();

        while(iterator.hasNext()) {
            String value = (String) iterator.next();
            stringBuilder.append(value);
            stringBuilder.append(delimiter);
        }

        if (stringBuilder.length() > 0) {
            // 删除末尾
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        }

        return stringBuilder.toString();
    }
}
